package com.haima.ziliao.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haima.common.annotation.Log;
import com.haima.common.core.controller.BaseController;
import com.haima.common.core.domain.AjaxResult;
import com.haima.common.enums.BusinessType;
import com.haima.ziliao.domain.PAds;
import com.haima.ziliao.service.IPAdsService;
import com.haima.common.utils.poi.ExcelUtil;
import com.haima.common.core.page.TableDataInfo;

/**
 * 广告管理Controller
 * 
 * @author hima
 * @date 2025-01-07
 */
@RestController
@RequestMapping("/ziliao/ads")
public class PAdsController extends BaseController
{
    @Autowired
    private IPAdsService pAdsService;

    /**
     * 查询广告管理列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:ads:list')")
    @GetMapping("/list")
    public TableDataInfo list(PAds pAds)
    {
        startPage();
        List<PAds> list = pAdsService.selectPAdsList(pAds);
        return getDataTable(list);
    }


    @GetMapping("/getIdByFlag")
    public AjaxResult getIdByFlag(PAds pAds)
    {
        PAds pads1 = pAdsService.getOne(new QueryWrapper<PAds>().eq("page",pAds.getPage()));
        return AjaxResult.success(pads1);
    }

    /**
     * 导出广告管理列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:ads:export')")
    @Log(title = "广告管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PAds pAds)
    {
        List<PAds> list = pAdsService.selectPAdsList(pAds);
        ExcelUtil<PAds> util = new ExcelUtil<PAds>(PAds.class);
        util.exportExcel(response, list, "广告管理数据");
    }

    /**
     * 获取广告管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ziliao:ads:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pAdsService.selectPAdsById(id));
    }

    /**
     * 新增广告管理
     */
    @PreAuthorize("@ss.hasPermi('ziliao:ads:add')")
    @Log(title = "广告管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PAds pAds)
    {
        List<PAds> list = pAdsService.selectPAdsList(pAds);
        if(list.size()>0){
            return AjaxResult.error("该页面广告已存在");
        }
        return toAjax(pAdsService.insertPAds(pAds));
    }

    /**
     * 修改广告管理
     */
    @PreAuthorize("@ss.hasPermi('ziliao:ads:edit')")
    @Log(title = "广告管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PAds pAds)
    {
        return toAjax(pAdsService.updatePAds(pAds));
    }

    /**
     * 删除广告管理
     */
    @PreAuthorize("@ss.hasPermi('ziliao:ads:remove')")
    @Log(title = "广告管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pAdsService.deletePAdsByIds(ids));
    }
}
